<template>
  <div class="dashboard">
    <!-- 统计卡片 -->
    <div class="stats-grid">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon orders">
            <el-icon><Document /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalOrders }}</div>
            <div class="stat-label">总订单数</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon revenue">
            <el-icon><Money /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ formatMoney(stats.totalRevenue) }}</div>
            <div class="stat-label">总收入</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon profit">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ formatMoney(stats.totalProfit) }}</div>
            <div class="stat-label">总利润</div>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon rate">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-value">{{ stats.profitRate }}%</div>
            <div class="stat-label">利润率</div>
          </div>
        </div>
      </el-card>
    </div>
    
    <!-- 图表区域 -->
    <div class="charts-grid">
      <el-card class="chart-card">
        <template #header>
          <span>订单趋势</span>
        </template>
        <div class="chart-container">
          <v-chart :option="orderTrendOption" />
        </div>
      </el-card>
      
      <el-card class="chart-card">
        <template #header>
          <span>利润分析</span>
        </template>
        <div class="chart-container">
          <v-chart :option="profitAnalysisOption" />
        </div>
      </el-card>
    </div>
    
    <!-- 最近订单 -->
    <el-card class="recent-orders">
      <template #header>
        <span>最近订单</span>
      </template>
      <el-table :data="recentOrders" style="width: 100%">
        <el-table-column prop="order_id" label="订单号" width="180" />
        <el-table-column prop="mall_name" label="店铺" width="150" />
        <el-table-column prop="pay_amount" label="金额" width="120">
          <template #default="{ row }">
            {{ formatMoney(row.pay_amount) }}
          </template>
        </el-table-column>
        <el-table-column prop="order_status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.order_status)">
              {{ row.order_status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.create_time) }}
          </template>
        </el-table-column>
        <el-table-column prop="actual_profit" label="利润" width="120">
          <template #default="{ row }">
            <span :class="{ 'profit-positive': row.actual_profit > 0, 'profit-negative': row.actual_profit < 0 }">
              {{ formatMoney(row.actual_profit) }}
            </span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Document, Money, TrendCharts } from '@element-plus/icons-vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart, BarChart, PieChart } from 'echarts/charts'
import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import VChart from 'vue-echarts'
import * as dayjs from 'dayjs'

use([CanvasRenderer, LineChart, BarChart, PieChart, GridComponent, TooltipComponent, LegendComponent])

// 统计数据
const stats = ref({
  totalOrders: 1234,
  totalRevenue: 456789.12,
  totalProfit: 123456.78,
  profitRate: 27.1
})

// 最近订单数据
const recentOrders = ref([
  {
    order_id: 'DY202401010001',
    mall_name: '测试店铺1',
    pay_amount: 299.00,
    order_status: '已完成',
    create_time: '2024-01-01 10:00:00',
    actual_profit: 89.70
  },
  {
    order_id: 'DY202401010002',
    mall_name: '测试店铺2',
    pay_amount: 199.00,
    order_status: '待发货',
    create_time: '2024-01-01 11:00:00',
    actual_profit: 59.70
  },
  {
    order_id: 'DY202401010003',
    mall_name: '测试店铺3',
    pay_amount: 399.00,
    order_status: '已发货',
    create_time: '2024-01-01 12:00:00',
    actual_profit: 119.70
  }
])

// 订单趋势图表配置
const orderTrendOption = ref({
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['订单数', '收入']
  },
  xAxis: {
    type: 'category',
    data: ['1月', '2月', '3月', '4月', '5月', '6月']
  },
  yAxis: [
    {
      type: 'value',
      name: '订单数'
    },
    {
      type: 'value',
      name: '收入(万元)'
    }
  ],
  series: [
    {
      name: '订单数',
      type: 'line',
      data: [120, 132, 101, 134, 90, 230]
    },
    {
      name: '收入',
      type: 'line',
      yAxisIndex: 1,
      data: [2.3, 2.8, 2.1, 2.9, 1.8, 4.2]
    }
  ]
})

// 利润分析图表配置
const profitAnalysisOption = ref({
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '利润构成',
      type: 'pie',
      radius: '50%',
      data: [
        { value: 1048, name: '商品利润' },
        { value: 735, name: '运费收入' },
        { value: 580, name: '平台补贴' },
        { value: 484, name: '其他收入' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
})

// 格式化金额
const formatMoney = (amount: number) => {
  return `¥${amount.toFixed(2)}`
}

// 格式化日期
const formatDate = (date: string) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    '已完成': 'success',
    '待发货': 'warning',
    '已发货': 'primary',
    '已取消': 'danger'
  }
  return statusMap[status] || 'info'
}

onMounted(() => {
  // TODO: 加载实际数据
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
  color: white;
}

.stat-icon.orders {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.revenue {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.profit {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.rate {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.chart-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.chart-container {
  height: 300px;
}

.recent-orders {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.profit-positive {
  color: #67c23a;
}

.profit-negative {
  color: #f56c6c;
}
</style> 